<html><head>
	<meta content="text/html; charset=utf-8" http-equiv="Content-Type"> 
	<script src='<?=_P("application/views/scripts/jquery-1.5.1.js")?>'></script> 
</head>
<body>

<script src="<?=_U("script/models/db")?>"></script>
<script>
<?echo "var current_class_id='$current_class_id';"?>

var tmp = new Array();
$(document).ready(function(){
	// alert('abc') ;
	$('.cls_add').css('cursor','pointer');
	$('.cls_add').attr('is_adding','false');
	$('.cls_add').click(function(){
		if($('.cls_add').attr('is_adding') == 'true')
		{
			alert('请先处理完此次添加');
			return; 
		}
		else{
			$('.cls_add').attr('is_adding','true');
		}
		var line = $("<div class='webim_class'></div>");
		var span_title = $("<span></span>");
		var input = $("<input type='text' value=''/>");
		span_title.append(input);
		line.append(span_title) ; 
		var span_btn_edit = $('<span class="edit">完成</span>');
		span_btn_edit.css('cursor','pointer') ; 
		span_btn_edit.click(function(){
			var new_title = input.val();
			db.insert('webim_class',{title:new_title},function(data){
				if(data.result)
				{
					window.location.href = window.location.href ;
				}
				else
				{
					alert(data) ;
				}
			});
		});
		line.append(span_btn_edit) ;  
		$('#webim_class_container').append(line);
	});
	
	$('.webim_class .delete').css('cursor','pointer');
	$('.webim_class .delete').attr('is_deleted','false');
	$('.webim_class .delete').click(function(){
		var me = $(this);
		var prev = $(this).prev().prev();
		var prev_id = prev.attr('id');  //cls_1
		var prev_id_segs = prev_id.split('_') ; 
		var cls_id = prev_id_segs[prev_id_segs.length-1];
		// alert(cls_id);
		db.del('webim_class',{id:cls_id},function(data){
			if(data.result)
			{
				me.parent().remove();
			}
			else
			{
				alert(data)  ; 
			}
		});
	});
	
	$('.webim_class .edit').css('cursor','pointer');
	$('.webim_class .edit').attr('is_edit','false');
	$('.webim_class .edit').click(function(){
		var prev = $(this).prev();
		var prev_id = prev.attr('id');  //cls_1
		var prev_id_segs = prev_id.split('_') ; 
		var cls_id = prev_id_segs[prev_id_segs.length-1];
		// alert(cls_id);return;
		// alert($(this).attr('is_edit'));
		if($(this).attr('is_edit')=='true')
		{
			var new_title = $('#text_' + prev_id).val();  
			var me = $(this);
			db.update('webim_class',{title:new_title},{id:cls_id},function(data){
				if(data.result)
				{
					me.text('编辑');
					var ori_html = tmp[prev_id];
					var ori_ob = $(ori_html);
					ori_ob.text(new_title);
					prev.html('');
					prev.append(ori_ob) ;    
					me.attr('is_edit','false');   
				} 
				else
				{
					alert('更新失败:' + data);    
				}
			}); 
		}
		else
		{
			$(this).text('完成'); 
			// alert(prev_id);
			var txt= prev.find('a').text();
			// alert(txt);
			tmp[prev_id] = prev.html();
			
			prev.html("<input type='text' id='text_" + prev_id + "' value='" + txt + "'/>") ;
			$(this).attr('is_edit','true');
		}
		// alert(tmp[prev_id]);
		//$prev.html('abc');
	});
	
	$('.im_add').css('cursor','pointer');
	$('.im_add').attr('is_im_adding','false');
	$('.im_add').click(function(){
		if($('.im_add').attr('is_im_adding') == 'true')
		{
			alert('请先处理完此次添加');
			return; 
		}
		else{
			$('.im_add').attr('is_im_adding','true');
		}
		//alert('abc') ;
		//return;
		var line = $("<div class='webim'></div>");
		
		var span_show_html = $("<span></span>");
		var textarea = $("<textarea cols='60' rows='10'/>");
		span_show_html.append(textarea);
		line.append(span_show_html) ; 
		var span_btn_edit = $('<span class="edit">完成</span>');
		span_btn_edit.css('cursor','pointer') ; 
		span_btn_edit.click(function(){
			var new_html = textarea.val();
			new_html1 = new_html.replace(/\"/g,'\\\"');
			new_html1 = new_html1.replace(/\'/g,"\\\'");  
			new_html1 = new_html1.replace(/\r\n/g,"");   
			new_html1 = new_html1.replace(/\t/g,"");
			var p = {show_html:new_html1,class_id:current_class_id};
			//alert(p.show_html);
			//alert(p.class_id);
			//return ; 
			db.insert('webim',p,function(data){
				if(data.result)
				{
					window.location.href = window.location.href ;
				}
				else
				{
					alert(data) ;
				}
			});
		});
		line.append(span_btn_edit) ;  
		$('#webim_container').append(line);
	});
	
	$('.webim .edit').css('cursor','pointer');
	$('.webim .edit').attr('is_edit','false');
	$('.webim .edit').click(function(){
		//alert('abc') ;
		var prev = $(this).prev();
		var prev_id = prev.attr('id');  //im_1
		var prev_id_segs = prev_id.split('_') ; 
		var im_id = prev_id_segs[prev_id_segs.length-1];
		if($(this).attr('is_edit')=='true')
		{
			var new_html = $('#area_' + im_id).val();
			new_html1 = new_html.replace(/\"/g,'\\\"');
			new_html1 = new_html1.replace(/\'/g,"\\\'");  
			new_html1 = new_html1.replace(/\r\n/g,"");   
			new_html1 = new_html1.replace(/\t/g,"");   
			//new_html1 = new_html1.replace(/\r/g,"");    
			//new_html1 = new_html1.replace(/\n/g,"");  
			//alert(new_html);
			//return;
			var me = $(this);
			// alert(new_html);
			db.update('webim',{show_html:new_html1},{id:im_id},function(data){
				if(data.result)
				{
					//alert(new_html);
					//return;
					me.text('编辑');
					prev.html(new_html);
					me.attr('is_edit','false'); 
					
				} 
				else
				{
					alert('更新失败:' + data);    
				}
			});
		}
		else
		{
			$(this).text('完成'); 
			 //alert(prev_id);
			 //return;
			var html = prev.html();
			 //alert(txt);
			//tmp[im_id] = html;
			//alert(im_id);
			prev.html("<textarea cols='60' rows='10' id='area_" + im_id + "'>" + html + "</textarea>") ;
			$(this).attr('is_edit','true');
		}
	});
	
	$('.webim .delete').css('cursor','pointer');
	$('.webim .delete').attr('is_deleted','false');
	$('.webim .delete').click(function(){
		var me = $(this);
		var prev = $(this).prev().prev();
		var prev_id = prev.attr('id');  //cls_1
		var prev_id_segs = prev_id.split('_') ; 
		var cls_id = prev_id_segs[prev_id_segs.length-1];
		//alert(cls_id);
		//return;
		db.del('webim',{id:cls_id},function(data){
			if(data.result)
			{
				me.parent().remove();
			}
			else
			{
				alert(data)  ; 
			}
		});
	});
	
});
	
</script>
<style>
.line11{width:630px}
.webim_class_cont{width:300px;float:left;}
	.webim_class_cont .cur{background:#efefef;}
	.cls_add,.im_add{padding-left:15px;color:red;}
.webim_cont{float:right;width:300px}
	.webim{border:1px solid #afafaf;margin-bottom:5px;}
.webim_class,.webim{line-height:32px;height:32px;color:#888888;font-size:12px;padding-left:5px}
	.webim_class input{width:150px;border:1px solid #AAA;height:23px;background:url(<?=_P("application/modules/webim_panel/views/front/images/input.png")?>);}
	.webim_class .edit,.webim_class .delete,.webim .edit,.webim .delete{background-color:#efefef;padding:3px 8px;border:1px solid #afafaf}
	.webim_class span a{text-decoration:none;padding:3px 6px;color:red;
	border:1px solid #AAA;background:url(<?=_P("application/modules/webim_panel/views/front/images/titlebg.png")?>);}
	webim_class a:hover{color:blue}
</style>
<div class="line11">
	<div class="webim_class_cont" id="webim_class_container">
		<div style="background:#efefef;width:298px;border:1px solid #afafaf;margin-bottom:5px;padding:3px 0px;">
			<span class="cls_add">增加</span>
		</div>
		<div style="border:1px solid #afafaf;">
			<?php foreach($webim_classes as $webim_class):?>
			<div class="webim_class<?php if($current_class_id==$webim_class->id){ echo " cur";}?>">
				<span id="cls_<?=$webim_class->id?>"><a href="<?=_U("module/webim_panel?class-id=$webim_class->id")?>"><?=$webim_class->title?></a></span>
				<span class="edit">编辑</span> <span class="delete">删除</span> 
			</div>
			<?php endforeach;?>
		</div>
	</div>
	<div class="webim_cont" id="webim_container">
		<div style="background:#efefef;width:298px;border:1px solid #afafaf;margin-bottom:5px;padding:3px 0px;">
			<span class="im_add">增加</span>
		</div>
		<?php foreach($current_class_webims as $webim):?>
		<div class="webim">
			<span id="im_<?=$webim->id?>"><?=$webim->show_html?></span>
			<span class="edit">编辑</span> <span class="delete">删除</span>
		</div> 
		<?php endforeach;?>
	</div>
</div>
	
</body>